package com.bwie.conferenceroom.service.impl;

import com.bwie.common.domain.ConferenceRoom;
import com.bwie.common.domain.Equipment;
import com.bwie.common.domain.request.ConferenceRoomReq;
import com.bwie.common.domain.request.SaveOrUpdConferenceRoomReq;
import com.bwie.common.domain.response.ConferenceRoomRes;
import com.bwie.common.domain.response.MeetingRoomAreaRes;
import com.bwie.common.domain.response.OccupationRes;
import com.bwie.common.domain.response.UpdConferenceRoomRes;
import com.bwie.conferenceroom.mapper.ConferenceRoomMapper;
import com.bwie.conferenceroom.service.ConferenceRoomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ConferenceRoomServiceImpl implements ConferenceRoomService {
    @Autowired
    private ConferenceRoomMapper conferenceRoomMapper;

    @Autowired
    private KafkaTemplate<String,Object> kafkaTemplate;

    /**
     * 获取会议室列表
     * @param conferenceRoomReq
     * @return
     */
    @Override
    public List<ConferenceRoomRes> getConferenceRoomList(ConferenceRoomReq conferenceRoomReq) {
        return conferenceRoomMapper.getConferenceRoomList(conferenceRoomReq);
    }

    /**
     * 获取所有会议室设备
     * @return
     */
    @Override
    public List<Equipment> getEquipment() {
        return conferenceRoomMapper.getEquipment();
    }

    /**
     * 获取所有区域以及楼栋
     * @return
     */
    @Override
    public List<MeetingRoomAreaRes> getMeetingRoomArea() {
        //查询所有区域
        List<MeetingRoomAreaRes> list = conferenceRoomMapper.getAllMeetingRoomArea(0);

        //获取各个区域下的楼栋
        if (!CollectionUtils.isEmpty(list)){
            for (MeetingRoomAreaRes meetingRoomAreaRes : list) {
                List<MeetingRoomAreaRes> allMeetingRoomArea = conferenceRoomMapper.getAllMeetingRoomArea(meetingRoomAreaRes.getValue());
                meetingRoomAreaRes.setChildren(allMeetingRoomArea);
            }
        }

        return list;
    }

    /**
     * 新建/修改会议室
     * @param saveOrUpdConferenceRoomReq
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdConferenceRoom(SaveOrUpdConferenceRoomReq saveOrUpdConferenceRoomReq) {
        if (saveOrUpdConferenceRoomReq.getId()!=null){
            //修改会议室
            conferenceRoomMapper.updConferenceRoom(saveOrUpdConferenceRoomReq);
            //根据会议室id删除会议室设备中间表
            conferenceRoomMapper.delConferenceRoomAndEquipment(saveOrUpdConferenceRoomReq.getId());
            //新增会议室设备中间表
            conferenceRoomMapper.addConferenceRoomAndEquipment(saveOrUpdConferenceRoomReq.getId(),saveOrUpdConferenceRoomReq.getEquipmentIds());
            ConferenceRoomReq conferenceRoomReq = new ConferenceRoomReq();
            if (conferenceRoomReq==null){
                kafkaTemplate.send("updConferenceRoom",conferenceRoomReq);
            }
        }else {
            //新建会议室
            conferenceRoomMapper.addConferenceRoom(saveOrUpdConferenceRoomReq);
            //新增会议室设备中间表
            conferenceRoomMapper.addConferenceRoomAndEquipment(saveOrUpdConferenceRoomReq.getId(),saveOrUpdConferenceRoomReq.getEquipmentIds());
            ConferenceRoomReq conferenceRoomReq = new ConferenceRoomReq();
            if (conferenceRoomReq==null){
                kafkaTemplate.send("updConferenceRoom",conferenceRoomReq);
            }
        }
    }

    /**
     * 根据id查询会议室详情
     * @param id
     * @return
     */
    @Override
    public UpdConferenceRoomRes selectConferenceRoomById(Integer id) {
        UpdConferenceRoomRes updConferenceRoomRes = new UpdConferenceRoomRes();
        //根据id查询一条会议室信息
        ConferenceRoom conferenceRoom = conferenceRoomMapper.selectConferenceRoomById(id);
        BeanUtils.copyProperties(conferenceRoom,updConferenceRoomRes);
        //根据Id查询会议室区域pid
        Integer meetingRoomAreaId = conferenceRoomMapper.selectMeetingRoomAreaIdByPid(conferenceRoom.getMeetingRoomAreaId());
        Integer[] meetingRoomAreaIds={meetingRoomAreaId,conferenceRoom.getMeetingRoomAreaId()};
        //根据会议室id查询会议室设备
        List<Integer> equipmentIds=conferenceRoomMapper.selectEquipmentIdsByConferenceRoomId(id);
        updConferenceRoomRes.setEquipmentIds(equipmentIds);
        updConferenceRoomRes.setMeetingRoomAreaIds(meetingRoomAreaIds);
        return updConferenceRoomRes;
    }

    /**
     * 获取占用会议室列表
     * @return
     */
    @Override
    public List<OccupationRes> getOccupationList() {
        return conferenceRoomMapper.getOccupationList();
    }
}
